#pragma once 

#include <iostream>
#include <pthread.h>
#include <string>
#include <ctime>
using namespace std;

typedef void (*callback_t)();
static int num = 1;

class Thread
{
public:
    Thread(callback_t cb)
        : _tid(0)
        , _name("")
        , _timestamp(0)
        , _isrunning(false)
        , _cb(cb)
    {}

    static void* Handle(void* args)
    {
        Thread* td = static_cast<Thread*>(args);
        td->Enter();
        return nullptr;
    }

    void Run()
    {
        _name = "thread-" + to_string(num++);
        _timestamp = (unsigned int)time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Handle, this);
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
        _isrunning = false;
    }

    string Name()
    {
        return _name;
    }

    uint64_t Timestamp()
    {
        return _timestamp;
    }

    bool IsRunning()
    {
        return _isrunning;
    }

    void Enter()
    {
        _cb();
    }

    ~Thread()
    {}

private:
    pthread_t _tid;
    string _name;
    uint64_t _timestamp;
    bool _isrunning;    
    callback_t _cb;
};
